package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/product")
public class SpuManageController {

    @Autowired
    private ManageService manageService;

    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){
        //查询所有销售属性集合
      List<BaseSaleAttr> baseSaleAttrList =   manageService.getBaseSaleAttrList();
      return Result.ok(baseSaleAttrList);
    }

    @GetMapping("{page}/{limit}")
    public Result getSpuInfoPage(@PathVariable long page,
                                 @PathVariable long limit,
                                 SpuInfo spuInfo){
        //创建一个page对象
        Page<SpuInfo> spuInfoPage = new Page<>(page, limit);
        //获取数据
        IPage<SpuInfo> spuInfoIPageList = manageService.getSpuInfoPage(spuInfoPage,spuInfo);
        return Result.ok(spuInfoIPageList);
    }

    //  /admin/product/saveSpuInfo
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        //  调用服务层方法
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //  /admin/product/spuImageList/{spuId}
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
        //  调用方法
        List<SpuImage> spuImageList = manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //  /admin/product/spuSaleAttrList/{spuId}
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        //  调用服务层方法
        List<SpuSaleAttr> spuSaleAttrList = manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
}